﻿using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1._Controls
{
	public partial class InfoResourceThumbnailGenerator : System.Web.UI.Page
	{
		private LoadedValue<int> loadedResourceId;
		public int ResourceId
		{
			get
			{
				if (loadedResourceId.Loader == null)
					loadedResourceId.Loader = () =>
						{
							int value = int.Parse(Request.QueryString["ResourceId"]);
							if (value <= 0) throw new Exception("输入了非法的资源标识。");
							return value;
						};
				return loadedResourceId.Value;
			}
		}

		private LoadedValue<string> loadedCopyName;
		public string CopyName
		{
			get
			{
				if (loadedCopyName.Loader == null)
					loadedCopyName.Loader = () => Request.QueryString["CopyName"];
				return loadedCopyName.Value;
			}
		}

		private LoadedValue<string> loadedResourceType;
		public string ResourceType
		{
			get
			{
				if (loadedResourceType.Loader == null)
					loadedResourceType.Loader = () => Request.QueryString["ResourceType"];
				return loadedResourceType.Value;
			}
		}

		private LoadedValue<DateTime?> loadedModifyTime;
		public DateTime? ModifyTime
		{
			get
			{
				if (loadedModifyTime.Loader == null)
					loadedModifyTime.Loader = () => DateTimeExtensions.ParseOrNull(Request.Headers["If-Modified-Since"]);
				return loadedModifyTime.Value;
			}
		}

		private LoadedValue<string> loadedETag;
		public string ETag
		{
			get
			{
				if (loadedETag.Loader == null)
					loadedETag.Loader = () => Request.Headers["ETag"];
				return loadedETag.Value;
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			string url = WebResourceCache.GetResourceCacheUrl(ResourceId, CopyName, ResourceType);
			Response.Redirect(url, true);


			/*InfoResources.InfoResourceBaseInfo resourceInfo = InfoResources.GetBaseInfoItem(ResourceId);
			if (resourceInfo == null) return;

			if (ModifyTime != null && resourceInfo.CreationDate.ToString() == ModifyTime.Value.ToString())
			{
				Response.StatusCode = 304;
				Response.StatusDescription = "Not Modified";
				return;
			}

			if (!string.IsNullOrEmpty(CopyName))
			{
				if (resourceInfo != null)
				{
					ResourceThumbnailCopyElement copyConfig = ApplicationConfiguration.GetResourceThumbnailCopy(CopyName);
					ImageCopyGenerator copyGenerator = new ImageCopyGenerator();
					copyGenerator.ResizeType = ResizeRectangleType.ZoomOut;
					copyGenerator.Width = copyConfig.Width;
					copyGenerator.Height = copyConfig.Height;

					InfoResourceCopy copy = InfoResourceCopies.GenerateCopy(resourceInfo.ResourceId, CopyName, copyGenerator);

					if (copy != null && copy.CopyContent != null)
					{
						OutputContent(resourceInfo.CreationDate, copy.CopyType, copy.CopyContent.ToArray());
					}
				}
			}
			else
			{
				InfoResource resource = InfoResources.GetItem(ResourceId);
				if (resource != null && resource.Content != null)
				{
					OutputContent(resource.CreationDate, resource.ResourceType, resource.Content.ToArray());
				}
			}

			// base.Render(writer);*/
		}

		/*protected void OutputContent(DateTime modifyTime, string resourceType, byte[] content)
		{
			Response.ClearContent();
			Response.Cache.SetETag(modifyTime.Ticks.ToString());
			Response.Cache.SetLastModified(modifyTime);
			Response.Cache.SetCacheability(HttpCacheability.Public);

			Response.ContentType = ApplicationConfiguration.GetMiniType(resourceType);
			Response.BinaryWrite(content);
		}*/
	}
}